﻿
using OpenCvSharp;
using System;
using System.IO;
using System.Windows;
using Xc.DataAccess.CodeReader.Properties;
using Xc.DataAccess.Core;

namespace Xc.DataAccess.CodeReader.Models.Filters
{
    [Filter("叠加",FilterType.Algorithm)]
    public class AlgorithmAdd:FilterBase
    {
        [InputParameter("图片")]
        public Mat Image2 { get; set; } = new Mat();
        [InputParameter("遮罩")] 
        public Mat Mask { get; set; } = new Mat();
        [InputParameter("类型")]
        public int DType { get; set; } = -1;
        internal override ResultData<Mat> UseChannel(Mat mat, FilterBase[] filterInfos, FilterBase preFilterBase)
        {
            Mat dst = new Mat();
            Cv2.Add(mat, Image2,dst, Mask,DType);
            return ResultData<Mat>.Accept(TextResource.filter_action_complete, dst);
        }
    }
    /// <summary>
    /// 人脸识别
    /// </summary>
    [Filter("人脸识别", FilterType.Algorithm)]
    public class AlgorithmFaceDetection : FilterBase
    {
        public AlgorithmFaceDetection()
        {
            FaceModel  = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources","haarcascade_frontalface_alt.xml");
        }
        /// <summary>
        /// 模型文件
        /// </summary>
        [InputParameter("模型文件")]
        public string FaceModel { get; set; }
        /// <summary>
        /// ScaleFactor
        /// </summary>
        [InputParameter("ScaleFactor")]
        public double ScaleFactor { get; set; } = 1.1D;
        /// <summary>
        /// MinNeighbors
        /// </summary>
        [InputParameter("MinNeighbors")] 
        public int MinNeighbors { get; set; } = 3;
        /// <summary>
        /// 识别类型
        /// </summary>
        [InputParameter("识别类型")]
        public HaarDetectionTypes HaarDetectionType { get; set; } = (HaarDetectionTypes)0;
        /// <summary>
        /// 最小大小
        /// </summary>
        [InputParameter("最小大小")]
        public CVSize MinSize { get; set; } = null;
        /// <summary>
        /// 最大大小
        /// </summary>
        [InputParameter("最大大小")]
        public CVSize MaxSize { get; set; } = null;
        /// <summary>
        /// 颜色
        /// </summary>
        [InputParameter("颜色")] 
        public Scalar Color { get; set; }
        private bool IsInit = false;
        CascadeClassifier cascadeClassifier = new CascadeClassifier();

        internal override ResultData<Mat> UseChannel(Mat mat, FilterBase[] filterInfos, FilterBase preFilterBase)
        {
            if (IsInit==false)
            {
                IsInit =cascadeClassifier.Load(FaceModel);
            }
            Mat dectect = new Mat();
            Cv2.CvtColor(mat,dectect,ColorConversionCodes.BGR2GRAY);
           var rects=  cascadeClassifier.DetectMultiScale(dectect, ScaleFactor, MinNeighbors, HaarDetectionType, MinSize, MaxSize);
            foreach (var item in rects)
            {
                Cv2.Rectangle(mat,item, Color);
            }
            return ResultData<Mat>.Accept(TextResource.filter_action_complete, mat);
        }
    }

}
